import { Component, OnInit } from '@angular/core';
import { MyModalHelper } from '@core/my-modal.helper';
import { CustomerOriginLinkComponent } from '../../customer-origin-link/customer-origin-link.component';
import { SalesmanService } from '@core/routes-services/salesman.service';
import { Router } from '@angular/router';
import { NzMessageService } from 'ng-zorro-antd';
import { MarketFansService } from '@core/routes-services/market-fans.service';
import { FansSalesmanEditComponent } from '../edit/edit.component';
import { myValidNull } from '@core/util/string-utils';
import { MarketFansChangeParentComponent } from '../change-parent/change-parent.component';
import { InteractionModalComponent } from '@shared/interaction-modal/interaction-modal.component';

@Component({
  selector: 'app-market-fans-list',
  templateUrl: './market-fans-list.component.html',
})
export class MarketFansListComponent implements OnInit {
  isLoading = false;
  keyword: any;
  salesmanId: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };


  allSalesmans: any [] = [];

  constructor(private message: NzMessageService,
              private router: Router,
              private marketFansService: MarketFansService,
              private salesmanService: SalesmanService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initOptions();
    this.initPage();
  }

  initOptions() {
    this.salesmanService.getSalesman().then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.allSalesmans = res.data.marketSalesmanList;
      } else {
        this.message.warning(res.message);
      }
    }, error => {
      this.message.error(error.message ? error.message : error.error);
    });
  }

  sortMap = {
    totalRedpack: null,
    totalIntegral: null,
    teamSales: null,
    totalProfit: null,
    lastPayTime: 'descend',
    lastLoginTime: null,
  };
  sortName = null;
  sortValue = null;

  sort(sortName: string, value: string): void {
    this.sortName = sortName;
    this.sortValue = value ? value : 'ascend';
    for (const key in this.sortMap) {
      this.sortMap[key] = null;
      this.sortMap[key] = (key === sortName ? this.sortValue : null);
    }
    this.search();
  }

  initPage() {
    this.isLoading = true;
    this.marketFansService.pagination(this.page, this.keyword, this.salesmanId, this.sortName, this.sortValue).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }


  seeLink = (entity) => {
    this.modalHelper.open(CustomerOriginLinkComponent, { customerId: entity.id }, 680, {
      nzTitle: '用户推广链',
    }).subscribe(result => {

    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  auditingEntity = (entity, map, reason) => {
    if (myValidNull(reason)) {
      this.updateEntity(entity, map);
    } else {
      this.modalHelper.open(InteractionModalComponent, { remark: entity[reason] }, 630, {
        nzTitle: '备注说明',
      }).subscribe(result => {
        if (result) {
          console.log(result);
          map[reason] = result;
          this.updateEntity(entity, map);
        }
        console.log('关闭弹窗');
      }, (error) => {
        console.error('弹窗回调出错：', error);
      });
    }
  };

  updateEntity = (entity, map) => {
    this.marketFansService.updateEntity(entity.id, map).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.message.success('操作成功');
        this.changePageNo(this.page.number);
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  };

  toGenerateSalesman = (entity) => {
    this.modalHelper.open(FansSalesmanEditComponent, { customerId: entity.id }, 620, {
      nzTitle: '设置账号',
    }).subscribe(result => {
      console.log('关闭弹窗');
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };

  toChangeParent = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    this.modalHelper.open(MarketFansChangeParentComponent, { entity: bak }, 630, {
      nzTitle: '修改直接推荐人',
    }).subscribe(result => {
      if (result) {
        temp ? this.search() : this.changePageNo(this.page.number);
      }
      console.log('关闭弹窗');
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };
  setAuth = (entity,beAuth) => {
    this.marketFansService.setAuth(entity.id, beAuth).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.message.success('操作成功');
        this.changePageNo(this.page.number);
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });

  };
}
